package org.netEase.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.netEase.dao.adverDao;
import org.netEase.dao.impl.adverDaoImple;
import org.netEase.entity.adver;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
@WebServlet(urlPatterns = { "/backstageManager/tuadd.shtml" })
public class advertable extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  SmartUpload smartUpload = new SmartUpload();
			smartUpload.initialize(this, request, response);
			try {
				smartUpload.upload();
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
			Request req = smartUpload.getRequest();
			String img =req.getParameter("img");
			String state=req.getParameter("state");
			Files files = smartUpload.getFiles();
			File file = files.getFile(0);
			adver ad= new adver();
			ad.setImg(img);
			ad.setState(state);
			if(file.isMissing() == false){
				try {
					file.saveAs("./images/index/"+file.getFileName());
				} catch (SmartUploadException e) {
					e.printStackTrace();
				}
				
				ad.setImg(file.getFileName());

			}
			adverDao addao= new adverDaoImple();
			int result=addao.add(ad);
			if(result > 0){
				response.sendRedirect(request.getContextPath() + "/backstageManager/adver.shtml");
			}
			
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
